<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Expense extends Api
{

    /**
     * Expense模型对象
     */
    protected $model = null;
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Expense();

    }

    /**
     * 查看
     */
    public function index()
    {
        $score = Db::name('user')->where('id', $this->auth->id)->value('score');


        $type = $this->request->request('type/d');
        if ($type == 1) {
            //积分记录

//            $data = $this->model->where('user_id', $this->auth->id)->order('id desc')->select();
            $userScoreLogM = new \app\common\model\UserScoreLog();
            $this->dataLimit = true;

            list($where, $sort, $order, $offset, $limit , $field) = $this->buildparams();

            $total = $userScoreLogM
                ->where($where)
                ->order($sort, $order)
                ->count();

            $userScoreLogList = $userScoreLogM
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

//            $data = $userScoreLogList;
            $data = [];
            foreach ($userScoreLogList as $k => $v){
                $data[$k] = $v;
                $data[$k]['account'] = $v['score'];
                $data[$k]['note'] = $v['memo'];
            }



        } else {
            //提现
            $tixianM = new \app\common\model\Tixian();
            $data = $tixianM->field('id,user_id,account,createtime,status')->where('user_id', $this->auth->id)->order('id desc')->select();

            foreach ($data as $k => $v) {
                $data[$k] = $v;
                $data[$k]['note'] = $v['status_text'];
            }
        }

        $this->success('', ['score' => $score, 'data' => $data]);

    }


    /**
     * 查看
     */
    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);

        if (!$row)
            $this->error(__('No Results were found'));


        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }


}
